//
//  LJTakePhotosUtil.m
//  LajinLive
//
//  Created by 刘洋 on 16/9/6.
//  Copyright © 2016年 lajin. All rights reserved.
//

#import "IGTakePhotosUtil.h"
#import "TZImageManager.h"

typedef void(^ImageViewBlock)(NSDictionary *);
@interface IGTakePhotosUtil ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, copy) ImageViewBlock imagePickerBlock;

@end

@implementation IGTakePhotosUtil

+ (void)ig_showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType
                                       isAllowEdit:(BOOL)isAllowEdit
                                  imagePickerBlock:(nullable void(^)(NSDictionary<NSString *,id> * _Nullable))imagePickerBlock
{
    IGTakePhotosUtil *util = [[IGTakePhotosUtil alloc] init];
    [util showImagePickerControllerWithSourceType:sourceType
                                      isAllowEdit:isAllowEdit
                                 imagePickerBlock:imagePickerBlock];
}

+ (void)ig_showImagePickerControllerWithImagePickerBlock:(nullable void(^)(NSDictionary<NSString *,id> * _Nullable))imagePickerBlock
{
    //默认是 UIImagePickerControllerSourceTypeCamera NO
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    BOOL isAllowEdit = NO;
    
    IGTakePhotosUtil *util = [[IGTakePhotosUtil alloc] init];
    [util showImagePickerControllerWithSourceType:sourceType
                                      isAllowEdit:isAllowEdit
                                 imagePickerBlock:imagePickerBlock];
}

+ (void)ig_showAssetsPickerControllerWithMaximumNumberOfSelection:(int)maximumNumberOfSelection assetsPickerBlock:(nullable void(^)(NSArray<UIImage *> * _Nullable))assetsPickerBlock
{
    IGTakePhotosUtil *util = [[IGTakePhotosUtil alloc] init];
    [util showAssetsPickerControllerWithMaximumNumberOfSelection:maximumNumberOfSelection isAllowEdit:NO assetsPickerBlock:assetsPickerBlock];
}

+ (void)ig_showAssetsPickerControllerWithIsAllowEdit:(BOOL)isAllowEdit assetsPickerBlock:(nullable void(^)(NSArray<UIImage *> * _Nullable))assetsPickerBlock {
    IGTakePhotosUtil *util = [[IGTakePhotosUtil alloc] init];
    [util showAssetsPickerControllerWithMaximumNumberOfSelection:1 isAllowEdit:YES assetsPickerBlock:assetsPickerBlock];
}

+ (void)ig_showAssetsForProductPickerControllerWithMaximumNumberOfSelection:(int)maximumNumberOfSelection assetsPickerBlock:(nullable void(^)(NSArray<UIImage *> * _Nullable, NSArray * _Nullable))assetsPickerBlock {
    IGTakePhotosUtil *util = [[IGTakePhotosUtil alloc] init];
    [util showAssetsForProductPickerControllerWithMaximumNumberOfSelection:maximumNumberOfSelection  assetsPickerBlock:assetsPickerBlock];
}

#pragma mark - privite
- (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType
                                    isAllowEdit:(BOOL)isAllowEdit
                               imagePickerBlock:(nullable void(^)(NSDictionary<NSString *,id> * _Nullable))imagePickerBlock
{
    self.imagePickerBlock = imagePickerBlock;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        picker.sourceType = sourceType;
    }
    picker.delegate = self;
    picker.allowsEditing = isAllowEdit;
    picker.bk_didFinishPickingMediaBlock = ^(UIImagePickerController *bkpicker, NSDictionary *info){
        [bkpicker dismissViewControllerAnimated:YES completion:^{
            if (imagePickerBlock) {
                imagePickerBlock(info);
            }
        }];
    };
    picker.bk_didCancelBlock = ^(UIImagePickerController *bkpicker) {
        [bkpicker dismissViewControllerAnimated:YES completion:nil];
    };
    
    [UIViewController ig_presentViewController:picker animated:YES completion:nil];
}

- (void)showAssetsPickerControllerWithMaximumNumberOfSelection:(int)maximumNumberOfSelection
                                                   isAllowEdit:(BOOL)isAllowEdit
                                             assetsPickerBlock:(nullable void(^)(NSArray<UIImage *> * _Nullable))assetsPickerBlock
{
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:maximumNumberOfSelection delegate:nil];
    picker.allowTakePicture = NO;
    picker.allowPickingVideo = NO;
    picker.isSelectOriginalPhoto = YES;
    picker.barItemTextColor = [UIColor appColor];
    if (isAllowEdit) {
        picker.showSelectBtn = NO;
        picker.allowCrop = YES;
        picker.cropRect = CGRectMake(0, (KSCREENHEIGHT - KSCREENWIDTH)/2, KSCREENWIDTH, KSCREENWIDTH);
    }
    
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos,NSArray *assets,BOOL isSelectOriginalPhoto) {
        if (photos.count != 0 && assetsPickerBlock) {
            assetsPickerBlock(photos);
        }
    };
    
    [UIViewController ig_presentViewController:picker animated:YES completion:nil];
}

- (void)showAssetsForProductPickerControllerWithMaximumNumberOfSelection:(int)maximumNumberOfSelection
                                                       assetsPickerBlock:(nullable void(^)(NSArray<UIImage *> * _Nullable, NSArray * _Nullable))assetsPickerBlock
{
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:maximumNumberOfSelection delegate:nil];
    picker.allowTakePicture = NO;
    picker.allowPickingVideo = NO;
    picker.isSelectOriginalPhoto = YES;
    picker.barItemTextColor = [UIColor appColor];
    
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets,BOOL isSelectOriginalPhoto) {
        if (photos.count != 0 && assetsPickerBlock) {
            if (photos.count == 1) {
                [[TZImageManager manager] getOriginalPhotoWithAsset:assets[0] completion:^(UIImage *photo, NSDictionary *info) {
                    assetsPickerBlock(@[photo], assets);
                }];
            } else {
                assetsPickerBlock(photos, assets);
            }
        }
    };
    
    [UIViewController ig_presentViewController:picker animated:YES completion:nil];
}

@end
